:: Written by: Joshua Cronk
:: This program will create the bat and sc file used to transfer folders
:: and files to the CAEN servers.
@echo off

Setlocal EnableDelayedExpansion

SET "version=1.0-beta"   	&:20130204 Joshua   initial version, creates basic files
SET "version=1.1-beta"   	&:20130206 Joshua   created menu, checked for errors in input
SET "version=1.2-beta"   	&:20130220 Joshua   new transfer works, deleting also works
SET "version=1.3-beta"   	&:20130220 Joshua   added user interface, fixed some errors in new transfer
SET "version=1.3.1-beta"   	&:20130221 Joshua   fixed minor bug in new transfer, added the default user error

color 07
title Easy Transfer - Version %version%
:menuLOOP
	set INIFILE="config.ini"
	call:getvalue %INIFILE% "default" 	"currentuser" DEFAULT
	call:getvalue %INIFILE% "username" 	"currentuser" CURENTUSER
	call:getvalue %INIFILE% "beastname" "currentuser" BEASTNAME
	call:getvalue %INIFILE% "umichname" "currentuser" UMICHNAME
	mode con: cols=71 lines=50
	echo +--------------------------------------------------------------------+
	echo ^|                         Easy CMD - Transfer                        ^|
	echo ^|                             Beta 1.3.1                             ^|
	echo ^|                      Written By: Joshua Cronk                      ^|
	echo ^|                             2/21/2013                              ^|
	echo +--------------------------------------------------------------------+
	echo.
	lib\chgcolor 04
	echo                                           Current User : %CURENTUSER%
	lib\chgcolor 07
	echo = Menu ===============================================================
	echo.
	dir bin\ /b /-p /o:gn > "transfers.dat"
	for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo.  %%B  %%C
	set choice=
	echo.&set /p choice=Make a choice or hit ENTER to quit: ||(
    	exit
	)
	echo.&call:menu_%choice%
GOTO:menuLOOP

:menu_1 Add User
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                         Add A New User                             ^|
	echo +--------------------------------------------------------------------+
	
	IF NOT EXIST users md users

	set /p name=Please enter your name:
	set /p beast=Please enter The Beast user name:
	set /p umich=Please enter umich user name:
	set /p current=Set as current user? (y/n):

	if "%current%" == "y" (
		echo ; This contains the current user that is set> config.ini
		echo [currentuser]>> config.ini
		echo default=0 >> config.ini
		echo username=%name%>> config.ini
		echo beastname=%beast%>> config.ini
		echo umichname=%umich%>> config.ini
	)

	set filename=%name: =_%
	
	echo [userinfo]>  users/%filename%.dat
	echo username=%name%>> users/%filename%.dat
	echo beastname=%beast%>> users/%filename%.dat
	echo umichname=%umich%>> users/%filename%.dat

	CLS
goto menuLOOP	

:menu_2 New Transfer
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                      Create A New Transfer                         ^|
	echo +--------------------------------------------------------------------+

	IF NOT EXIST scripts md scripts
	IF NOT EXIST bin md bin

	if "%DEFAULT%" == "1" (
		lib\chgcolor 04
		echo The current user is still default. Until current user has been changed you will have to type in user info by hand!
		lib\chgcolor 07
		goto enterInfo
	) ELSE (
		goto setCurrent
	)
	:setCurrent
		set /p current=Use current user info? (y/n):
		IF "%current%"=="y" goto skipInfo
		
	:enterInfo
		set /p user1=Please enter The Beast user name:
		set /p user2=Please enter umich user name:
		goto serverInfo

	:skipInfo
		set user1 = %BEASTNAME%
		set user2 = %UMICHNAME%
		echo %user1%
		echo %user2%
		pause
	
	:serverInfo
		set /p folder=Please enter folder name (or directory) to transfer:
		set /p ip=Please enter server address:

	set check1=\
	set check2=/

	set s=@@@%folder%
	if "%s%"=="!s:@@@%check2%=!" (
		goto checkNext
	) ELSE (
		goto deleteFront
	)

	checkNext:
	if "%s%"=="!s:@@@%check1%=!" (
		goto checkEnd	
	) ELSE (
		goto deleteFront
	)

	:deleteFront
	set folder=%folder:~1%
	goto checkEnd

	:checkEnd
	set checkend=%folder:~-1%
	IF %checkend% == / goto setFolders
	IF %checkend% == \ goto setFolders
	
	set folder=%folder%/
	
	:setFolders
		set folder_unix=%folder:\=/%
		set folder_win=%folder:/=\%
		set name2=%folder_unix:/=_%
		set name=%ip:.=_%
	:writeScript

		echo option confirm off 									>  scripts/%name2%%name%.sc
		echo open scp://%user1%@%ip% 								>> scripts/%name2%%name%.sc
		echo cd %folder_unix% 										>> scripts/%name2%%name%.sc
		echo option transfer binary 								>> scripts/%name2%%name%.sc
		echo get * c:\Transfer\%folder_win%							>> scripts/%name2%%name%.sc
		echo close 													>> scripts/%name2%%name%.sc
		echo open scp://%user2%@sftp.itcs.umich.edu 				>> scripts/%name2%%name%.sc
		echo cd Private/			 								>> scripts/%name2%%name%.sc
		echo put c:\Transfer\%folder_win%*	 						>> scripts/%name2%%name%.sc
		echo exit 													>> scripts/%name2%%name%.sc

	:writeBatch

		echo @echo off 												>  bin/%name2%%name%.bat
		echo title The Beast File Transfer 							>> bin/%name2%%name%.bat
		echo color 17					 							>> bin/%name2%%name%.bat
		echo echo Transfer starting...... 							>> bin/%name2%%name%.bat
		echo lib\winscp.com /script=scripts\%name2%%name%.sc		>> bin/%name2%%name%.bat
		echo if errorlevel 1 goto error 							>> bin/%name2%%name%.bat
		echo echo.													>> bin/%name2%%name%.bat
		echo pause 													>> bin/%name2%%name%.bat
		echo exit 													>> bin/%name2%%name%.bat
		echo :error 												>> bin/%name2%%name%.bat
		echo echo Transfer failed! 									>> bin/%name2%%name%.bat
		echo pause													>> bin/%name2%%name%.bat
	CLS
goto menuLOOP

:menu_3 Run Transfer
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                         Run A Transfer                             ^|
	echo +--------------------------------------------------------------------+
	set /a number = 1
	for /f "delims=" %%a in (transfers.dat) DO (call :printTransfers %%a)
	echo.&set /p runchoice=Select a script to run or hit ENTER to go back: ||(
    	CLS
    	goto menuLOOP
	)
	set /a runnumber = 1
	for /f "delims=" %%a in (transfers.dat) DO (CALL :checkNumber %%a)
goto menu_3

:menu_4 Delete Transfer
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                        Delete A Transfer                           ^|
	echo +--------------------------------------------------------------------+
	dir bin\ /b /-p /o:gn > "transfers.dat"
	set /a number = 1
	for /f "delims=" %%a in (transfers.dat) DO (call :printTransfers %%a)
	echo.&set /p delchoice=Select a script to delete or hit ENTER to go back: ||(
    	CLS
    	goto menuLOOP
	)
	set /a delnumber = 1
	for /f "delims=" %%a in (transfers.dat) DO (call :deleteTransfer %%a %delchoice%)
goto menu_4

:menu_5 Edit Transfer
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                         Edit A Transfer                            ^|
	echo +--------------------------------------------------------------------+
	lib\chgcolor 04
	echo Under construction!
	lib\chgcolor 07
	echo.&set /p editchoice=Hit ENTER to go back: ||(
    	CLS
    	goto menuLOOP
	)
goto menuLOOP

:menu_6 Change Current User
	CLS
	echo +--------------------------------------------------------------------+
	echo ^|                      Change Current User                           ^|
	echo +--------------------------------------------------------------------+
	::dir users\ /b /-p /o:gn > "user.dat"
	::set /a number = 1
	::for /f "delims=" %%a in (user.dat) DO (call :printTransfers %%a)
	::echo.&set /p userchoice=Select the user you wish to be current or hit ENTER to go back: ||(
    ::	CLS
    ::	goto menuLOOP
	::)
	::set /a currnumber = 1
	::for /f "delims=" %%a in (user.dat) DO (call :setCurrent %%a %userchoice%)
	lib\chgcolor 04
	echo Under construction!
	lib\chgcolor 07
	echo.&set /p editchoice=Hit ENTER to go back: ||(
    	CLS
    	goto menuLOOP
	)
goto menuLOOP

:: Helper functions

:printTransfers
:: This function will print out the transfer name with menu number
:: %1 = the transfer name
	echo  %number% %1
	set /a number+=1
goto :eof

:checkNumber
:: This function will check if the current transfer
:: is the run choice
:: %1 = the transfer name
	IF "%runchoice%"=="%runnumber%" (
			start cmd /c bin\%1
			cls
			goto :menu_3
		)
	set /a runnumber+=1
goto :eof

:deleteTransfer
:: This function will check if the current transfer
:: is the deletion choice
:: %1 = the transfer name
:: %2 = the number selected
	set script=%1
	set script=%script:.bat=%
	set script=%script%.sc
	IF "%2"=="%delnumber%" (
		rm bin\%1
		rm scripts\%script%
		cls
		goto :menu_4
	)
	set /a delnumber+=1
goto :eof

:setCurrent
:: This function will check if the current transfer
:: is the run choice
:: %1 = the user name
:: %2 = the number selected
	IF "%runchoice%"=="%runnumber%" (
			start cmd /c bin\%1
			cls
			goto :menu_3
		)
	set /a runnumber+=1
goto :eof

:getvalue
:: This function reads a value from an INI file and stored it in a variable
:: %1 = name of ini file to search in.
:: %2 = search term to look for
:: %3 = group name (not currently used)
:: %4 = variable to place search result
	FOR /F "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~4=%%~j
goto:eof
